package com.stone.gateway.config;

import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.reactor.filter.SaReactorFilter;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.Set;

/**
 * @author fxj
 * SaToken配置类
 * date 2021-09-08
 */

@Configuration
public class SaTokenConfigure {

    @Autowired
    GwProperties gwyProperties;

    /**
     * 注册 Sa-Token全局过滤器
     * @return SaReactorFilter
     */
    @Bean
    public SaReactorFilter getSaReactorFilter() {
        List<String> whiteList = gwyProperties.getWhiteList();
        SaReactorFilter saReactorFilter = new SaReactorFilter();
        // 拦截地址
        saReactorFilter.addInclude("/**");
        // 开放地址
        for (String whiteStr:whiteList){
            saReactorFilter.addExclude(whiteStr);
        }
        //登录验证
        return saReactorFilter.setAuth(r -> checkPermission())
                .setError(e -> SaResult.error(e.getMessage()));
    }

    public static void checkPermission() {
        String menuId = SaHolder.getRequest().getHeader("MENU_ID");
        String requestPath = SaHolder.getRequest().getRequestPath();
        SaSession saSession = StpUtil.getSession();
        Set<String> perms = (Set<String>) saSession.get("perms");
    }
}